package com.lh.serverforwebrtc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.lh.serverforwebrtc.bean.RespBean;
import com.lh.serverforwebrtc.bean.RoomBean;
import com.lh.serverforwebrtc.mapper.RoomMapper;
import com.lh.serverforwebrtc.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: Lh
 * @Description:
 * @Date: Created in 11:25 2022/6/10
 */
@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Override
    public RespBean createRoom(Long id) {
        RoomBean room = new RoomBean();
        Double roomID = (Math.random() * 9 + 1) * 100000;
        room.setRoomId(roomID.longValue());
        room.setRoomNumber(1L);
        List<Long> stringList = new ArrayList<>();
        stringList.add(id);
        room.setRoomMember(JSON.toJSONString(stringList));
        room.setRoomCreateDate(new Date());
        room.setCreatorId(id);
        roomMapper.createRoom(room);
        return RespBean.success("创建成功",room);
    }

    @Override
    public RespBean deleteRoom(Long roomId) {
        roomMapper.deleteRoom(roomId);
        return RespBean.success("删除成功");
    }

    @Override
    public RespBean addRoom(Long id, Long roomId) {
        //查询房间是否存在
        RoomBean room = roomMapper.findRoom(roomId);
        if (room != null){
            List<Long> members = JSON.parseArray(room.getRoomMember(), Long.class);
            members.add(id);
            room.setRoomMember(JSON.toJSONString(members));
            room.setRoomNumber(Long.valueOf(members.size()));
            roomMapper.updateRoom(room);
            return RespBean.success("加入成功",room);
        }
        return RespBean.error("加入失败");
    }

    //判断挂断的人是房主还是成员
    //1.成员挂断，则就只是成员退出房间，但是房主已久还在房间，但要给出，谁退出聊天的提示，更新当前房间信息
    //2. 房主挂断，房间全部人都退出，并且销毁该房间信息
    @Override
    public RespBean hangUp(Long id, Long roomId) {
        try {
            RoomBean room = roomMapper.findRoom(roomId);
            if (room.getCreatorId() == id){
                roomMapper.deleteRoom(roomId);
                return RespBean.success("退出成功",room.getRoomMember());
            }else {
                room.setRoomNumber(room.getRoomNumber()-1);
                room.getRoomMember();
                List<Long> members = JSON.parseArray(room.getRoomMember(), Long.class);
                if (members.contains(id)){
                    members.remove(id);
                }
                room.setRoomMember(JSON.toJSONString(members));
                roomMapper.updateRoom(room);
                return RespBean.success("退出成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.error("退出失败");
        }
    }

    /**
     * 通过房间号查询房间成员
     * @param roomId 房间号
     * @return 返回成员列表
     */
    @Override
    public List<Long> findMembersByRoomId(Long roomId) {
        String membersByRoomId = roomMapper.findMembersByRoomId(roomId);
        try {
            JSONArray.parseArray(membersByRoomId, Long.class);
        }catch (Exception e){
            e.printStackTrace();
        }
        return JSONArray.parseArray(membersByRoomId, Long.class);
    }

}
